﻿--[[
   @brief    helper_mgr user for  start, stop, switch for helper.
   @author   harleyxwang
   @version  1.0
   @date     2016-03-03
--]]

helper_mgr_control =
{
    plugin_start_fail_code = 1;
    plugin_terminate_code = 2;
    plugin_not_implement = 3;
    plugin_interface_version_dismatch = 2;
    last_game_id = GAME_ID.INVALID_GAME_ID;

    init = function()
        base_utility.log("helper_mgr_control.init called");
        ierd_tgp.tpf_ui.Require("helper_mgr\\model.lua");
       
    end;

    handle_banner_switch = function(event_id, json_str)
        ierd_tgp.tpf_ui.Log("[helper_mgr]handle_banner_switch, " .. tostring(json_str));
        local params = base_utility.json_to_table(json_str);
        local game_id = nil;
        if params ~= nil then
            if params.module_id ~= nil then
                game_id = tonumber(params.module_id);
            end
        end
      
        if game_id == nil or game_id == 0 then
            ierd_tgp.tpf_ui.Log("[helper_mgr.close_current_helper]game_id is nil");
            return
        end

        if helper_mgr_control.last_game_id ~= game_id then
            ierd_tgp.tpf_ui.Log("[helper_mgr.close_current_helper]game_id is NOT same as last " ..
                tostring(helper_mgr_control.last_game_id));
            helper_mgr_control.close_current_helper();
        end

        local game_state = banner_model.get_game_state(game_id);
        local game_info = banner_model.get_game_base_info(game_id);

        if nil ~= game_state and 0 == game_state.iState then
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                game_lib:EnterHelperZone(game_id);
            end
        end

        helper_mgr_control.save_params_local_tbl(params);

        -- 判断url是否是空
       if params.module_url == nil or params.module_url == "" or 
            string.find(params.module_url, "platform/frames/building.html") ~= nil then
            local valid_url = false;
            local banner_info = banner_model.get_banner_module_info_by_game_id(game_id);
            if banner_info ~= nil and banner_info.module_url ~= nil and banner_info.module_url ~= "" then
                valid_url = true;
            end
           if not valid_url then
               helper_mgr_model.banner_url_empty_config[game_id] = params;
               base_utility.log("[helper_mgr]banner_url_empty, game_id="..game_id, true);   
           end
       end

        -- step1: 通知web进行页面的切换, open_link跳转页面也走这里
        -- 所以这里并不全是助手页面，很可能这里game_id并不对应真的游戏
        if params.without_notify_web == nil or params.without_notify_web == false then
            -- 框架第一次初始化，不需要再通知页面
            helper_mgr_control.open_web_page(game_id, params);
        end;

        -- step2: 通知插件是否拉取成功的通知

        -- 首先判断有没有插件，如果没有插件，则直接通知跳转页面
        -- 这里第二个参数0代表直接跳转
        local plugin_state_info = banner_model.get_plugin_state(game_id);
        local plugin_mgr =
            base_utility.get_component_adapter("IPlugin_mgr",
                "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
        if plugin_state_info == nil then
            local params = base_utility.json_to_table(json_str);
            helper_mgr_control.notify_page_plugin_state_helper(game_id, 0, params.module_url);
            plugin_mgr:SetCurrentGameID(game_id);
            return
        end

        local plugin_state = plugin_mgr:GetPluginState(game_id);
        if plugin_state == 1 then
            -- 插件已经准备好，直接通知
            helper_mgr_control.notify_page_plugin_state_helper(game_id, 0, params.module_url);
            plugin_mgr:SetCurrentGameID(game_id);
        else
            -- 打开助手进程
            helper_mgr_control.start_plugin_program(game_id);
        end
        
        helper_mgr_control.last_game_id = game_id;
    end;
    -- 启动助手成功
    switch_to_helper_page = function(event_id, json_str)
        if json_str == nil then return end
        local table_data = base_utility.json_to_table(json_str);
        local is_current_game = 0;
        if table_data ~= nil and table_data.is_current_game ~= nil then
            is_current_game = tonumber(table_data.is_current_game);
        end
        if is_current_game ~= 1 then
            return;
        end
        local game_id = nil;
        if table_data ~= nil and table_data.game_id ~= nil then
            game_id = tonumber(table_data.game_id);
        end
        local url = nil;
        if game_id ~= nil then
            local str_game_id = tostring(game_id);
            if helper_mgr_model.switch_data_table[str_game_id] ~= nil then
                url = helper_mgr_model.switch_data_table[str_game_id].url;
                helper_mgr_model.switch_data_table[str_game_id] = nil;
            end
        end
        base_utility.log("[helper_mgr, start_Pallas_plugin success!]");
        helper_mgr_control.notify_page_plugin_state_helper(game_id, 0, url);
    end;

    open_web_page = function(game_id_param, params)
        if game_id_param == nil or params == nil then
            return;
        end;
        local url = params.module_url;
        local table_params = {};
        local game_id = tostring(game_id_param);
        table_params = helper_mgr_model.switch_data_table[game_id];
       
        if table_params ~= nil and type(table_params) == 'table' and
            table_params.callback_func ~= nil and 
            table_params.callback_func ~= "" then
            local rsp_data_table = {};
            rsp_data_table.retCode = 0;
            rsp_data_table.msg = "";
            rsp_data_table.seq_num = table_params.seq_num;
            rsp_data_table.src = table_params.src;
            rsp_data_table.dest = 0;
            rsp_data_table.data = {};
            rsp_data_table.data.mid = tonumber(game_id_param);
            rsp_data_table.data.first = helper_mgr_model.first_open_web;
            if rsp_data_table.seq_num ~= nil then
                rsp_data_table.src = table_params.src;
            else  -- 是主动通知
                rsp_data_table.data.event = "jc_set_app_status";
                rsp_data_table.src = 0;
            end
            if url ~= nil then
                rsp_data_table.data.url = url;
            end

            local rsp_module_info = banner_model.get_web_rsp_module_info(game_id_param);
            if rsp_module_info ~= nil then
                rsp_data_table.data.info = rsp_module_info;
            end

            if params.check_attach ~= nil and params.check_attach == 1 and
               params.attach_info ~= nil and params.attach_info.select_id ~= nil then
                local attach_module_info = banner_model.get_web_rsp_module_info(params.attach_info.select_id);
                if attach_module_info ~= nil then
                    rsp_data_table.attach_data = {};
                    rsp_data_table.attach_data.mid = tonumber(params.attach_info.select_id);
                    rsp_data_table.attach_data.url = attach_module_info.module_url;
                    rsp_data_table.attach_data.info = attach_module_info;
                else
                    rsp_data_table.attach_data = {};
                    rsp_data_table.attach_data.mid = tonumber(params.attach_info.select_id);
                    rsp_data_table.attach_data.url = "";
                end
            end;

            local login_mgr =
                base_utility.get_component_adapter("ILogin_mgr",
                    "ierd_tgp.login_mgr.GetLoginMgr();");
            if login_mgr ~= nil then
                -- web_st
                local info = ierd_tgp.login_mgr.WebStPod:new();
                login_mgr:GetWebSt(info);
                rsp_data_table.data.web_st = info.web_st;
                rsp_data_table.data.uin = login_model.main_uin;
                info:delete();

                -- pkey/vas_key
                local client_st_info = ierd_tgp.login_mgr.ClientStPod:new();
                login_mgr:GetClientSt(client_st_info);
                rsp_data_table.data.pkey = client_st_info.client_st;
                client_st_info:delete();
            end;

            web_utility.lua_call_js_ex(table_params.callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
        helper_mgr_model.switch_data_table[game_id] = nil;
        helper_mgr_model.first_open_web = 0;
        base_utility.log("[helper_mgr] switch_to_helper_page"); 
    end;

    save_params_local_tbl = function(params)
        if params ~= nil then
            local tbl_params = {};
            if params.module_id ~= nil then
                tbl_params.game_id = tostring(params.module_id);
            end
            if params.module_url ~= nil then
                tbl_params.url = params.module_url;
            end
            if params.callback_func ~= nil then
                tbl_params.callback_func = params.callback_func;
            end
            if params.seq_num ~= nil then 
                tbl_params.seq_num = params.seq_num;
            end
            if params.src ~= nil then
                tbl_params.src = params.src;
            end
            -- 数据插入table中
            if tbl_params.game_id ~= nil then
               helper_mgr_model.switch_data_table[tbl_params.game_id] = tbl_params;
            end
        end
    end;

    start_plugin_program = function(game_id)
        -- 打开助手进程
        local server_id = 0;
        local game_servers = banner_model.get_game_servers(game_id);
        if game_servers ~= nil then
            server_id = game_servers.selleted;
        end

        local server_name = "";
        if  game_servers ~= nil and game_servers.server_count > 0 then
            for i = 1, game_servers.server_count do
                if server_id == game_servers.servers[i - 1].id then
                    server_name = game_servers.servers[i - 1].name;
                    break;
                end;
            end;
        end

        local plugin_mgr =
            base_utility.get_component_adapter("IPlugin_mgr",
                "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
        local pControl = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        if pControl == nil then return end
        local hwnd = pControl:GetHwnd();
        local plugin_hwnd = plugin_mgr:GetPluginHwnd(hwnd, 5);
        local result = false;
        ierd_tgp.tpf_ui.Log("[helper_mgr, open_Pallas_plugin]game_id="..game_id.." server_id ="..
            server_id.." server_name="..server_name);
        result = plugin_mgr:fire_game_launch_open_event(game_id, plugin_hwnd, server_id, 
            server_name, 0, 0, 0, 0, result);
        if false == result then
            -- 启动助手失败, 等回调事件再通知web
            base_utility.log("[helper_mgr], start plugin failer, game_id="..game_id);     
        end;
    end;

    notify_page_plugin_terminate = function(event_id, json_str)
        if json_str == nil then return end
        local table_data = base_utility.json_to_table(json_str);
        local game_id = nil;
        if table_data ~= nil and table_data.game_id ~= nil then
            game_id = tonumber(table_data.game_id);
        end
        helper_mgr_control.notify_page_plugin_state_helper(game_id,
            helper_mgr_control.plugin_terminate_code);
    end;
    
    notify_page_plugin_launch_fail = function(event_id, json_str)
        if json_str == nil then return end
        local table_data = base_utility.json_to_table(json_str);
        local game_id = nil;
        if table_data ~= nil and table_data.game_id ~= nil then
            game_id = tonumber(table_data.game_id);
        end
        local err_code = helper_mgr_control.plugin_start_fail_code;
        if table_data ~= nil and table_data.error_code ~= nil then
            err_code = tonumber(table_data.error_code);
            if helper_mgr_control.plugin_interface_version_dismatch == err_code then
                err_code = helper_mgr_control.plugin_not_implement;
            end
        end
        helper_mgr_control.notify_page_plugin_state_helper(game_id, err_code);
    end;

    -- 客户端唯一用来跳转页面的函数，本来是助手用来做页面切换的
    -- ret_code为0时，可以根据module_url做跳转
    notify_page_plugin_state_helper = function(game_id, ret_code, module_url)
        if game_id == nil or 0 == game_id then
            return
        end
        
        local url = module_url;
        if module_url == nil then
            url = "";
        end;
        
        local callback_func = web_register_center.get_func_by_browser(
            "plugin_life_cycle_event", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = {};
            rsp_data_table.retCode = ret_code;
            rsp_data_table.msg = "";
            rsp_data_table.src = 0;
            rsp_data_table.dest = 0;
            rsp_data_table.data = {};
            rsp_data_table.data.mid = tonumber(game_id);
            rsp_data_table.data.event = "plugin_life_cycle_event";
            rsp_data_table.data.url = url;
            web_utility.lua_call_js_ex(callback_func, 
                    base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        else
            base_utility.log("[helper_mgr], notify_page_plugin_state, callback is null");
        end

        local param_table = {};
        param_table["game_id"] = game_id;
        param_table["ret_code"] = ret_code;
        event_center.send_event(
            frame_bottom_event.PLUGIN_LIFE_CYCLE_EVENT, base_utility.table_to_json(param_table));
    end;

    update_plugin_state = function(event_id, json_str)
        local params = base_utility.json_to_table(json_str);
        if params.game_id == nil or 0 == params.game_id or
            banner_model.GAME_CENTER_GAME_ID == params.game_id then
            return
        end
        local plugin_state_info = banner_model.get_plugin_state(params.game_id);
        if plugin_state_info == nil then return end

        if ierd_tgp.game_library.KUPDATTING == plugin_state_info.status and 
            plugin_state_info.progress > 90 then
            helper_mgr_model.updating_plugin_tbl[tostring(params.game_id)] = 1;
        elseif ierd_tgp.game_library.KNORMAL == plugin_state_info.status and 
                1 == helper_mgr_model.updating_plugin_tbl[tostring(params.game_id)] then
            helper_mgr_model.updating_plugin_tbl[tostring(params.game_id)] = nil;
            -- 如果不是当前模块，则不处理返回
            local current_module_id = banner_view.get_current_select_module();

            local plugin_mgr =
                base_utility.get_component_adapter("IPlugin_mgr",
                    "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
            local plugin_state = plugin_mgr:GetPluginState(params.game_id);
            if current_module_id == params.game_id and plugin_state ~= 1 then
                -- 打开助手进程
                base_utility.log("[helper_mgr], update complete, start plugin, game_id = " 
                    .. params.game_id);
                helper_mgr_control.start_plugin_program(params.game_id);
            end
        end
    end;
        
    on_mainframe_mode_change = function(eid, json_str)
--        local param = base_utility.json_to_table(json_str);
--        if param and
--            mainframe_model.mainframe_mode.MINI_MODE == param.mainframe_mode then
--                base_utility.log("[helper_mgr.on_mainframe_mode_change]has changed to mini mode");

--                helper_mgr_control.close_current_helper();
--        end
    end;
    
    close_current_helper = function()
        ierd_tgp.tpf_ui.Log("[helper_mgr]close_current_helper");
        if GAME_ID.INVALID_GAME_ID == helper_mgr_control.last_game_id then
            ierd_tgp.tpf_ui.Log("[helper_mgr.close_current_helper]there's not current helper");
            return;
        end
        
        local plugin_mgr =
            base_utility.get_component_adapter("IPlugin_mgr",
                "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
        plugin_mgr:fire_game_launch_close_event(helper_mgr_control.last_game_id);
    end;

    on_update_banner_config = function(event_id, json_param)
        local game_id = banner_view.get_current_select_module();
        if game_id == nil then
            local data_out = comm_center.call_service("SvrGetCurrentNavTab", {});
            if data_out ~= nil and data_out["game_id"] ~= nil then
                game_id =  tonumber(data_out["game_id"]);
            end
        end
        if game_id == nil then return end
        local param_tbl = base_utility.json_to_table(json_param);
        if param_tbl ~= nil and param_tbl.game_id ~= nil then
            if param_tbl.game_id ~= 0 and param_tbl.game_id ~= game_id then
                return      -- 不是关心的game id有更新，则不处理
            end
        end
        if helper_mgr_model.banner_url_empty_config[game_id] ~= nil then
            local banner_info = banner_model.get_banner_module_info_by_game_id(game_id);
            if banner_info ~= nil and banner_info.module_url ~= nil and banner_info.module_url ~= "" then
                -- 刷新页面
                base_utility.log("[helper_mgr]on_update_banner_config, refresh page, game_id="..game_id, true);

                local params = helper_mgr_model.banner_url_empty_config[game_id];

                helper_mgr_model.banner_url_empty_config[game_id] = nil;    -- 清空数据
                if params ~= nil and type(params) == 'table' then
                    local rsp_data_table = {};
                    rsp_data_table.retCode = 0;
                    rsp_data_table.msg = "";
                    rsp_data_table.src = 0;
                    rsp_data_table.dest = 0;
                    rsp_data_table.data = {};
                    rsp_data_table.data.mid = tonumber(game_id);
                    rsp_data_table.data.first = helper_mgr_model.first_open_web;
                    rsp_data_table.data.event = "jc_set_app_status";
                    rsp_data_table.data.url = banner_info.module_url;
                    

                    local rsp_module_info = banner_model.get_web_rsp_module_info(game_id);
                    if rsp_module_info ~= nil then
                        rsp_data_table.data.info = rsp_module_info;
                    end

                    if params.check_attach ~= nil and params.check_attach == 1 and
                       params.attach_info ~= nil and params.attach_info.select_id ~= nil then
                        local attach_module_info = banner_model.get_web_rsp_module_info(params.attach_info.select_id);
                        if attach_module_info ~= nil then
                            rsp_data_table.attach_data = {};
                            rsp_data_table.attach_data.mid = tonumber(params.attach_info.select_id);
                            rsp_data_table.attach_data.url = attach_module_info.module_url;
                            rsp_data_table.attach_data.info = attach_module_info;
                        end
                    end;

                    local login_mgr =
                        base_utility.get_component_adapter("ILogin_mgr",
                            "ierd_tgp.login_mgr.GetLoginMgr();");
                    if login_mgr ~= nil then
                        -- web_st
                        local info = ierd_tgp.login_mgr.WebStPod:new();
                        login_mgr:GetWebSt(info);
                        rsp_data_table.data.web_st = info.web_st;
                        rsp_data_table.data.uin = login_model.main_uin;
                        info:delete();

                        -- pkey/vas_key
                        local client_st_info = ierd_tgp.login_mgr.ClientStPod:new();
                        login_mgr:GetClientSt(client_st_info);
                        rsp_data_table.data.pkey = client_st_info.client_st;
                        client_st_info:delete();
                    end;
                    local callback_func = web_register_center.get_func_by_browser(
                        "jc_set_app_status", CID_MAINFRAME_QBWEB);
                    if callback_func ~= nil then
                        web_utility.lua_call_js_ex(callback_func, 
                            base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
                        base_utility.log("[helper_mgr]on_update_banner_config, call jc_set_app_status event,"..callback_func, true);
                    else 
                        base_utility.log(
                            "[helper_mgr]on_update_banner_config, callback_func is nil.",true);
                    end;
                end
            end
        end
    end;
};

helper_mgr_control.init();
